<?php

declare (strict_types=1);

namespace App\Models\GameDB\Pet;

use App\Models\MainDB\Gift;
use App\Models\BaseModel;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class PoolGroup extends BaseModel
{
    protected $connection = 'mysql_game';
    protected $table = 'lottery_pet_pool_group';
    //protected $fillable = [];
    protected $guarded = [];

    //protected $casts = ['id' => 'int', 'user_id' => 'integer', 'amount' => 'integer', 'balance' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function one(array $filters)
    {
        return self::query()->where($filters)->first();
    }

    public function listAll(array $filters, array $fields)
    {
        return self::query()->where($filters)->select($fields)->get();
    }

    public function pool()
    {
        return $this->hasOne(Pool::class, 'id', 'pool_id');
    }

    // 批量增加礼物数量（抽完补奖池, 额外设置总数）
    public function batchUpdateDrawData(array $updateIOPut, array $incGroup, array $drawGroupCount)
    {
        if (empty($updateIOPut)) {
            return true;
        }

        $groupIdIds       = array_keys($updateIOPut['group_input']);
        foreach ($groupIdIds as $k => $val) {
            if ($val <= 0) {
                unset($groupIdIds[$k]);
            }
        }
        $groupIdIdsString = implode(',', $groupIdIds);

        $table = $this->getTable();
        $sql   = "update {$table} set ";
        // 增加输入
        $sql .= "`input` = case `id`";
        foreach ($updateIOPut['group_input'] as $groupId => $input) {
            if (!empty($groupId)) {
                $sql .= "when {$groupId} then `input` + {$input} ";
            }
        }
        $sql .= "else `input` end, ";
        // 增加输出
        $sql .= "`output` = case `id` ";
        foreach ($updateIOPut['group_output'] as $groupId => $output) {
            if (!empty($groupId)) {
                $sql .= "when {$groupId} then `output` + {$output} ";
            }
        }
        $sql .= "else `output` end, ";

        if (!empty($incGroup)) {
            // 增加round
            $sql .= "`round` = case `id` ";
            foreach ($incGroup as $groupId => $incCount) {
                if (!empty($groupId)) {
                    $sql .= "when {$groupId} then `round` + {$incCount} ";
                    if (!in_array($groupId, $groupIdIds)) {
                        $groupIdIdsString .= ','.$groupId;
                    }
                }
            }
            $sql .= "else `round` end, ";
        }

        if (!empty($drawGroupCount)) {
            // 增加round
            $sql .= "`count` = case `id` ";
            foreach ($drawGroupCount as $groupId => $groupIncCount) {
                if (!empty($groupId)) {
                    $sql .= "when {$groupId} then `count` + {$groupIncCount} ";
                }
            }
            $sql .= "else `count` end ";
        }

        // 条件
        $sql .= " where id in ($groupIdIdsString)";

        return DB::connection('mysql_game')->update($sql);
    }


}
